﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using GSQ.BusinessEntities;
using GSQ.BusinessLogic;

namespace UIapp.Dialogos
{
    public partial class frmFacturasAnular : Plantillas.frmDialogPlantilla, Interfaces.IPantalla
    {
        public frmFacturasAnular(FACTURAS entity)
        {
            InitializeComponent();
            this.Entity = entity;
            this.BindControls();
        }

        #region Variables
        public FACTURAS Entity;
        bool entityError = false;
        #endregion

        #region Implementacion IPantalla
        public int ComandosSoportados
        {
            get { return 0; }
        }

        public void Nuevo()
        {
            throw new NotImplementedException();
        }

        public void Modificar()
        {
            throw new NotImplementedException();
        }

        public void Eliminar()
        {
            throw new NotImplementedException();
        }

        public void Refrescar()
        {
            throw new NotImplementedException();
        }

        public void Buscar()
        {
            throw new NotImplementedException();
        }

        public void AgregarProducto(Controles.TransferEntity tranferEntity)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Metodos Auxiliares
        public void BindControls()
        {
            if (this.Entity != null)
            {
                this.lblNroFactura.Text = this.Entity.NRO_FACTURA.ToString();
                if (this.Entity.CONDICIONES != null)
                    this.lblCondicion.Text = this.Entity.CONDICIONES.CONDICION;
                this.lblFechaHora.Text = this.Entity.FECHA_HORA.ToString("dd/MM/yyyy hh:mm tt");
                this.lblCliente.Text = string.Format("{0}-{1}", this.Entity.ID_CLIENTE.ToString("0000"), this.Entity.CLIENTES.CLIENTE);
                this.lblVendedor.Text = string.Format("{0}-{1}", this.Entity.ID_VENDEDOR.ToString("0000"), this.Entity.VENDEDORES.VENDEDOR);
                this.lblMonto.Text = this.Entity.M_TOTAL.ToString("N2");
                this.lblMontoPagado.Text = this.Entity.M_PAGADO.ToString("N2");
            }
        }
        #endregion

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("¿Está seguro que desea anular la factura?", "Confirmación", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
                    == System.Windows.Forms.DialogResult.OK)
            {
                if (this.Entity.M_PAGADO > 0)
                {
                    if (MessageBox.Show("Está factura se encuentra pagada parcial o totalmente.\r\r¿Está seguro que la desea anular de todos modos?", "Confirmación", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
                                     == System.Windows.Forms.DialogResult.Cancel)
                    {
                        return;
                    }
                }
                this.backgroundWorker.RunWorkerAsync();
                Comun.ShowLoadingFRM();
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (new FacturasBL().Anular(this.Entity))

                entityError = false;
            else
                entityError = true; 
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
             Comun.CloseLoadingFRM();
            if (e.Error != null)
                Comun.ShowMessage(e.Error);
            else if (this.entityError)
                return;
            else
            {
                Comun.ShowInMainMessage("Factura anulada correctamente.");
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
    }
}
